//
//  ByCellShopCarSettleListItem.m
//  mijifen_ios
//
//  Created by 朱凯 on 2017/10/27.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByCellShopCarSettleListItem.h"

@implementation ByCellShopCarSettleListItem
{
    UIImageView *_mainView;             // 主图
    UIView      *_sepLine;              // 两个cell之间的分割
    
    UILabel     *_titleLabel;           // 标题
    UILabel     *_currentPriceLabel;    // 当前价格
    UILabel     *_originPriceLabel;     // 原价
    
    UILabel     *_countTitleLabel;      // 数量标题
    
    UILabel     *_transferLabel;        // 配送方式
    UILabel     *_additionLabel;        // 附加label（自取：显示地址，快递：显示当日达）
    
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self setupUI];
        [self layout];
    }
    return self;
}

- (void)setupUI {
    
    // 主图
    _mainView = [[UIImageView alloc] init];
    _mainView.backgroundColor = BY_BLUE_COLOR;
    _mainView.layer.cornerRadius = 4;
    _mainView.layer.masksToBounds = true;
    [self.contentView addSubview:_mainView];
    
    // 分割
    _sepLine = [[UIView alloc] init];
    _sepLine.backgroundColor = BY_LINE_COLOR;
    [self.contentView addSubview:_sepLine];
    
    // 标题
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.numberOfLines = 2;
    _titleLabel.font = FONT(14);
    _titleLabel.text = @"发了纠纷说多了纠纷说多了纠纷说多了纠纷说多了纠纷说多了纠纷说多了纠纷说多分";
    _titleLabel.textColor = [UIColor blackColor];
    _titleLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:_titleLabel];
    
    // 现价
    _currentPriceLabel = [[UILabel alloc] init];
    _currentPriceLabel.adjustsFontSizeToFitWidth = true;
    _currentPriceLabel.font = FONT(18);
    _currentPriceLabel.textColor = BY_RED_COLOR;
    _currentPriceLabel.text = @"￥88888.88";
    _currentPriceLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:_currentPriceLabel];
    
    // 原价
    _originPriceLabel = [[UILabel alloc] init];
    _originPriceLabel.adjustsFontSizeToFitWidth = true;
    _originPriceLabel.font = FONT(14);
    _originPriceLabel.textColor = [UIColor lightGrayColor];
    _originPriceLabel.textAlignment = NSTextAlignmentLeft;
    _originPriceLabel.text = @"￥99999.99";
    [self.contentView addSubview:_originPriceLabel];
    
    // 数量标题
    _countTitleLabel = [[UILabel alloc] init];
    _countTitleLabel.font = FONT(14);
    _countTitleLabel.textColor = [UIColor lightGrayColor];
    _countTitleLabel.textAlignment = NSTextAlignmentLeft;
    _countTitleLabel.text = @"购买数量：x1";
    [self.contentView addSubview:_countTitleLabel];
   
    // 配送方式
    _transferLabel = [[UILabel alloc] init];
    _transferLabel.font = FONT(14);
    _transferLabel.textColor = [UIColor lightGrayColor];
    _transferLabel.textAlignment = NSTextAlignmentLeft;
    _transferLabel.text = @"配送方式：自取";
    [self.contentView addSubview:_transferLabel];
    
    // 附加项
    _additionLabel = [[UILabel alloc] init];
    _additionLabel.backgroundColor = BY_THEME_COLOR;
    _additionLabel.layer.cornerRadius = 4;
    _additionLabel.layer.masksToBounds = true;
    _additionLabel.font = FONT(16);
    _additionLabel.textColor = BY_GOLD_COLOR;
    _additionLabel.textAlignment = NSTextAlignmentCenter;
    _additionLabel.text = @"当日达";
    [self.contentView addSubview:_additionLabel];
}

- (void)layout {
    
    [_mainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@10);
        make.top.equalTo(@10);
        make.bottom.equalTo(@-10);
        make.width.equalTo(_mainView.mas_height);
    }];
    [_sepLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(@0);
        make.height.equalTo(@1);
    }];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_mainView.mas_right).offset(10);
        make.top.equalTo(_mainView);
        make.right.equalTo(@-5);
    }];
    [_currentPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_titleLabel);
        make.top.equalTo(_titleLabel.mas_bottom).offset(5);
        make.right.equalTo(_originPriceLabel.mas_left).offset(-5);
    }];
    [_originPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(_currentPriceLabel);
        make.right.lessThanOrEqualTo(_titleLabel);
    }];
    [_countTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_titleLabel);
        make.height.equalTo(@26);
        make.top.equalTo(_currentPriceLabel.mas_bottom).offset(5);
        make.right.equalTo(_titleLabel);
    }];
    [_transferLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_countTitleLabel);
        make.top.equalTo(_countTitleLabel.mas_bottom).offset(5);
    }];
    [_additionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_transferLabel);
        make.top.equalTo(_transferLabel.mas_bottom).offset(5);
        make.size.mas_offset(CGSizeMake(100, 32));
    }];
    
}

#pragma mark - 设置属性
- (void)setModel:(ByModelShopCarProductInStore *)model {
    
    _model = model;
    
    [self setNeedsLayout];
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    // 标题
    _titleLabel.text = _model.title;
    // 现价
    _currentPriceLabel.text = [NSString stringWithFormat:@"￥%.2lf", _model.price];
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:_currentPriceLabel.text];
    [attrStr addAttribute:NSFontAttributeName value:FONT(14) range:[_currentPriceLabel.text rangeOfString:@"￥"]];
    [attrStr addAttribute:NSFontAttributeName value:FONT(14) range:NSMakeRange(_currentPriceLabel.text.length - 2, 2)];
    _currentPriceLabel.attributedText = attrStr;
    // 原价
    _originPriceLabel.text = [NSString stringWithFormat:@"￥%.2lf", _model.originPrice];
    attrStr = [[NSMutableAttributedString alloc] initWithString:_originPriceLabel.text];
    [attrStr addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid|NSUnderlineStyleSingle) range:[_originPriceLabel.text rangeOfString:_originPriceLabel.text]];
    _originPriceLabel.attributedText = attrStr;
    // 数量
    _countTitleLabel.text = [NSString stringWithFormat:@"购买数量：x%li", _model.count];
    // 配送方式
    _transferLabel.text = [NSString stringWithFormat:@"配送方式：%@", _model.transfer];
    // 附加项
    if ([_model.transfer containsString:@"自取"]) {
        _additionLabel.text = @"查看地址";
    } else {
        _additionLabel.text = @"当日达";
    }
}

@end
